/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lhh.apst.advancedpagerslidingtabstrip.slice;

import com.lhh.apst.advancedpagerslidingtabstrip.ResourceTable;
import com.lhh.apst.advancedpagerslidingtabstrip.fragments.FirstFragment;
import com.lhh.apst.advancedpagerslidingtabstrip.fragments.FourthFragment;
import com.lhh.apst.advancedpagerslidingtabstrip.fragments.SecondFragment;
import com.lhh.apst.advancedpagerslidingtabstrip.fragments.ThirdFragment;
import com.lhh.apst.library.AdvancedPagerSlidingTabStrip;
import com.lhh.apst.library.Margins;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;

import java.io.IOException;

/**
 * ViewTabSlice
 *
 * @since 2021-04-30
 */
public class ViewTabSlice extends AbilitySlice implements PageSlider.PageChangedListener {

    private AdvancedPagerSlidingTabStrip mAPSTS;
    private PageSlider mVP;

    private static final int VIEW_FIRST = 0;
    private static final int VIEW_SECOND = 1;
    private static final int VIEW_THIRD = 2;
    private static final int VIEW_FOURTH = 3;

    private static final int VIEW_SIZE = 4;

    private Component mFirstFragment = null;
    private Component mSecondFragment = null;
    private Component mThirdFragment = null;
    private Component mFourthFragment = null;

    @Override
    public void onPageSliding(int i, float v, int i1) {

    }

    @Override
    public void onPageSlideStateChanged(int i) {

    }

    @Override
    public void onPageChosen(int i) {

    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_icon_tab);
        findViews();
        initViews();
    }

    private void findViews() {
        mAPSTS = (AdvancedPagerSlidingTabStrip) findComponentById(ResourceTable.Id_tabs);
        mVP = (PageSlider) findComponentById(ResourceTable.Id_vp_main);
    }

    private void initViews() {
        mVP.setPageCacheSize(VIEW_SIZE);
        mVP.setProvider(new MySlidePagerProvider(this));
        mVP.setCurrentPage(VIEW_FIRST);

        mAPSTS.setViewPager(mVP);
        mAPSTS.setOnPageChangeListener(this);
        mAPSTS.showDot(VIEW_FIRST, "99+");
    }

    /**
     * MySlidePagerProvider
     *
     * @since 2021-04-30
     */
    public class MySlidePagerProvider extends PageSliderProvider implements AdvancedPagerSlidingTabStrip.ViewTabProvider, AdvancedPagerSlidingTabStrip.LayoutProvider {

        private final Context context;

        /**
         * 构造函数
         *
         * @param context 上下文
         */
        public MySlidePagerProvider(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public String getPageTitle(int position) {
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_FIRST:
                        return "first";
                    case VIEW_SECOND:
                        return "second";
                    case VIEW_THIRD:
                        return "third";
                    case VIEW_FOURTH:
                        return "fourth";
                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int position) {
            switch (position) {
                case VIEW_FIRST:
                    if (null == mFirstFragment) {
                        mFirstFragment = new FirstFragment(context).get();
                    }
                    componentContainer.addComponent(mFirstFragment);
                    return mFirstFragment;

                case VIEW_SECOND:
                    if (null == mSecondFragment) {
                        mSecondFragment = new SecondFragment(context).get();
                    }
                    componentContainer.addComponent(mSecondFragment);
                    return mSecondFragment;

                case VIEW_THIRD:
                    if (null == mThirdFragment) {
                        mThirdFragment = new ThirdFragment(context).get();
                    }
                    componentContainer.addComponent(mThirdFragment);
                    return mThirdFragment;

                case VIEW_FOURTH:
                    if (null == mFourthFragment) {
                        mFourthFragment = new FourthFragment(context).get();
                    }
                    componentContainer.addComponent(mFourthFragment);
                    return mFourthFragment;
                default:
                    break;
            }
            return null;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
            Component contentView = (Component) object;
            componentContainer.removeComponent(contentView);
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return component == o;
        }

        @Override
        public Component onSelectIconView(int position, Component view, ComponentContainer parent) {
            Component component = new Component(this.context);
            Element drawView = null;
            switch (position) {
                case VIEW_FIRST:
                    drawView = getResElement(ResourceTable.Media_home_categry_icon_f_n);
                    break;
                case VIEW_SECOND:
                    drawView = getResElement(ResourceTable.Media_home_auther_icon_f_p);
                    break;
                case VIEW_THIRD:
                    drawView = getResElement(ResourceTable.Media_home_live_icon_f_n);
                    break;
                case VIEW_FOURTH:
                    drawView = getResElement(ResourceTable.Media_home_main_icon_f_n);
                    break;
                default:
                    break;
            }
            component.setBackground(drawView);
            return component;
        }

        @Override
        public Component onIconView(int position, Component view, ComponentContainer parent) {
            Component component = new Component(this.context);
            Element drawView = null;
            switch (position) {
                case VIEW_FIRST:
                    drawView = getResElement(ResourceTable.Media_home_categry_icon_n);
                    break;
                case VIEW_SECOND:
                    drawView = getResElement(ResourceTable.Media_home_auther_icon_f_n);
                    break;
                case VIEW_THIRD:
                    drawView = getResElement(ResourceTable.Media_home_live_icon_n);
                    break;
                case VIEW_FOURTH:
                    drawView = getResElement(ResourceTable.Media_home_main_icon_n);
                    break;
                default:
                    break;
            }
            component.setBackground(drawView);
            return component;
        }

        @Override
        public DependentLayout.LayoutConfig getLayoutConfig() {
            return new DependentLayout.LayoutConfig(45, 40);
        }

        @Override
        public float getPageWeight(int position) {
            return 0;
        }

        @Override
        public int[] getPageRule(int position) {
            return new int[0];
        }

        @Override
        public Margins getPageMargins(int position) {
            return new Margins(0, 0, 0, 15);
        }
    }

    private Element getResElement(int resId) {
        Element element = null;
        try {
            element = new PixelMapElement(this.getResourceManager().getResource(resId));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return element;
    }
}

